using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class StartMenu : MonoBehaviour
{
	private const ulong APP_UID = 72067558858375168uL;

	[SerializeField]
	private List<ButtonWithIcon> btns;

	[SerializeField]
	private Button playBtn;

	[SerializeField]
	private Button settingsBtn;

	[SerializeField]
	private Button eShopBtn;

	[SerializeField]
	private Button quitBtn;

	[SerializeField]
	private SettingsPanel settingsPanel;

	[SerializeField]
	private GameObject loadingPanel;

	[SerializeField]
	private DynamicFilledImage loadingProgress;

	private int btnInd;

	private bool delay;

	private float timer;

	private bool isPanelActive;

	private bool loading;

	private static bool _firstInit = true;

	private int BtnIndex
	{
		get
		{
			return btnInd;
		}
		set
		{
			if (delay)
			{
				delay = false;
				int activeBtnInd = value;
				if (value < 0)
				{
					activeBtnInd = btns.Count - 1;
				}
				else if (value > btns.Count - 1)
				{
					activeBtnInd = 0;
				}
				btnInd = activeBtnInd;
				ShowActiveIcon(activeBtnInd);
			}
		}
	}

	private void ShowActiveIcon(int activeBtnInd)
	{
		foreach (ButtonWithIcon btn in btns)
		{
			btn.HideActiveIcon();
		}
		btns[activeBtnInd].ShowActiveIcon();
	}

	private void Awake()
	{
		loading = true;
	}

	private void Start()
	{
		Setup();
		if (_firstInit)
		{
			StartCoroutine(DelayInputRoutine());
		}
		else
		{
			loading = false;
		}
	}

	public void Setup()
	{
		loadingPanel.SetActive(value: false);
		base.gameObject.SetActive(value: true);
		isPanelActive = true;
		delay = true;
		timer = 0f;
		BtnIndex = 0;
		playBtn.onClick.RemoveAllListeners();
		eShopBtn.onClick.RemoveAllListeners();
		settingsBtn.onClick.RemoveAllListeners();
		quitBtn.onClick.RemoveAllListeners();
		playBtn.onClick.AddListener(delegate
		{
			StartCoroutine(LoadYourAsyncScene());
		});
		settingsBtn.onClick.AddListener(delegate
		{
			base.gameObject.SetActive(value: false);
			settingsPanel.Setup(isMainMenu: true);
		});
		quitBtn.onClick.AddListener(QuitGame);
		eShopBtn.onClick.AddListener(delegate
		{
		});
	}

	private void QuitGame()
	{
		Application.Quit();
	}

	private IEnumerator LoadYourAsyncScene()
	{
		loading = true;
		loadingPanel.SetActive(value: true);
		AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("GamePlay");
		asyncLoad.allowSceneActivation = true;
		while (!asyncLoad.isDone)
		{
			loadingProgress.FillAmount = asyncLoad.progress;
			yield return null;
		}
	}

	private IEnumerator DelayInputRoutine()
	{
		yield return new WaitUntil(() => SplashScreen.isFinished);
		yield return new WaitUntil(() => SceneManager.GetSceneByName("MainMenu").isLoaded);
		yield return new WaitForSeconds(0.3f);
		loading = false;
		_firstInit = false;
	}

	private void Update()
	{
		if (loading)
		{
			return;
		}
		if (Singleton<InputController>.Instance.Action)
		{
			btns[BtnIndex].btn.onClick.Invoke();
		}
		if (Singleton<InputController>.Instance.Up)
		{
			BtnIndex--;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Down)
		{
			BtnIndex++;
			delay = true;
		}
		if (Singleton<InputController>.Instance.Vertical == 0f)
		{
			delay = true;
			timer = 0f;
		}
		else if (Singleton<InputController>.Instance.Vertical < 0f)
		{
			BtnIndex++;
		}
		else if (Singleton<InputController>.Instance.Vertical > 0f)
		{
			BtnIndex--;
		}
		if (!delay)
		{
			timer += Time.deltaTime;
			if (timer > 0.3f)
			{
				delay = true;
				timer = 0f;
			}
		}
	}
}
